package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.HomeAuthorActivity;
import com.jss.lanouhn.fristapp.activity.HomeItemDetail;
import com.jss.lanouhn.fristapp.activity.HomeTitleActivity;
import com.jss.lanouhn.fristapp.modles.SearchRightData;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 16/9/8.
 */
public class SearchRightFragmentAdapter  extends RecyclerView.Adapter<SearchRightFragmentAdapter.SearchRightViewHolder>{
    private Context mContext;
    private SearchRightData data;

    public SearchRightFragmentAdapter(Context mContext, SearchRightData data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public SearchRightViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_big, null);
        SearchRightViewHolder holder = new SearchRightViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SearchRightViewHolder holder, int position) {
        final SearchRightData.DataBean.PostsBean bean=data.getData().getPosts().get(position);
        holder.tv.setText(bean.getColumn().getCategory());
        holder.tv_up_right.setText(bean.getColumn().getTitle());
        holder.tv_up_left.setText(bean.getAuthor().getNickname());
        holder.tv_down_right.setText(bean.getTitle());
        holder.tv_down_left.setText(bean.getLikes_count()+"");
        Picasso.with(mContext).load(bean.getAuthor().getAvatar_url()).into(holder.iv_small);
        Picasso.with(mContext).load(bean.getCover_image_url()).into(holder.iv_big);
        holder.tv_up_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, HomeTitleActivity.class);
                intent.putExtra("id",bean.getColumn().getId()+"");
                mContext.startActivity(intent);

            }
        });
        holder.tv_up_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, HomeAuthorActivity.class);
                intent.putExtra("id", ""+bean.getAuthor().getId());
                mContext.startActivity(intent);
            }
        });

        holder.tv_down_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, HomeItemDetail.class);
                intent.putExtra("id",""+bean.getId());
                mContext.startActivity(intent);
            }
        });
        holder.iv_big.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, HomeItemDetail.class);
                intent.putExtra("id",""+bean.getId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.getData().getPosts().size();
    }

    class SearchRightViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_up_right, tv_up_left, tv_down_right, tv_down_left,tv;
        private ImageView iv_big;
        private CircleImageView iv_small;

        public SearchRightViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.btn_home_item_big);
            tv_up_right = (TextView) itemView.findViewById(R.id.tv1_home_item_big);
            tv_up_left = (TextView) itemView.findViewById(R.id.tv2_home_item_big);
            tv_down_right = (TextView) itemView.findViewById(R.id.tv3_home_item_big);
            tv_down_left = (TextView) itemView.findViewById(R.id.tv4_home_item_big);
            iv_big = (ImageView) itemView.findViewById(R.id.iv_home_item_big);
            iv_small = (CircleImageView) itemView.findViewById(R.id.icon_home_item_big);
        }
    }
}
